package com.win.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.win.pojo.User;
import com.win.pojo.vo.UserVo;
import com.win.service.UserService;
import com.win.mapper.UserMapper;
import com.win.utils.JwtHelper;
import com.win.utils.MD5Util;
import com.win.utils.Result;
import com.win.utils.ResultCodeEnum;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 54478
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-05-22 09:01:55
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(queryWrapper);

        if (loginUser == null) {
            //说明没有该用户的信息
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //判断密码是否正确
        if (!(MD5Util.encrypt(user.getPassword()).equals(loginUser.getPassword()))) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //根据用户id生成token令牌
        String token = JwtHelper.createToken(loginUser.getUid().longValue());

        UserVo userVo = new UserVo();

        //对象属性拷贝
        BeanUtils.copyProperties(loginUser,userVo);
        userVo.setToken(token);

        Map<String,UserVo> map = new HashMap<>();
        loginUser.setPassword("");
        map.put("userInfo",userVo);
        return Result.ok(map);
    }

    @Override
    public Result register(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User registerUser = userMapper.selectOne(queryWrapper);

        if (registerUser != null) {
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        //先将用户的密码加密处理
        user.setPassword(MD5Util.encrypt(user.getPassword()));

        userMapper.insert(user);
        return Result.ok(null);

    }

    @Override
    public Result checkUser(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);

        User user = userMapper.selectOne(queryWrapper);
        if (user != null){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        return Result.ok(null);
    }

    @Override
    public Result getUserInfo(String token) {
        if (token != null && !token.isEmpty()){

            //1.检查用户token是否过期
            boolean res = JwtHelper.isExpiration(token);
            if (res){
                //说明过期了
                return Result.build(null,ResultCodeEnum.NOTLOGIN);
            }
            //2.根据token获取用户的id
            int uid = JwtHelper.getUserId(token).intValue();

            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUid,uid);
            User user = userMapper.selectOne(queryWrapper);
            return Result.ok(user);
        }
        return Result.build(null,ResultCodeEnum.NOTLOGIN);
    }
}




